﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace MacomberMapSystem.Common.Serialization
{
    /// <summary>
    /// This class holds information on a serializable target
    /// </summary>
    public class MM_Serializable_Target: Attribute
    {
         #region Variable declarations
        /// <summary>
        /// The collection of possible target types
        /// </summary>
        [Flags]
        public enum enumTargetType
        {
            /// <summary>Not a target</summary>
            None = 0,
            /// <summary>This item is a field in XML and RDB, and provides one of the unique points for it</summary>
            FieldUnique=1, 
            /// <summary>This item is a target for a field in XML and RDB</summary>
            FieldValue = 2,
            /// <summary>Within XML format, this is a field, in RDB in the parameter collection</summary>
            ParameterValue = 3,
            /// <summary>Within XML format, this is a sub-element of the parent element, in RDB in the Parameters table</summary>
            ParameterXml = 4,
            /// <summary>Elements are stored in a key/value relationship in MM and externally</summary>
            ParameterDictionary = 5
        }

        /// <summary>The target type</summary>
        public enumTargetType TargetType = enumTargetType.None;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new serializable target
        /// </summary>
        /// <param name="TargetType">The type of target</param>
        public MM_Serializable_Target(enumTargetType TargetType)
        {
            this.TargetType = TargetType;
        }
        #endregion

        #region Type determination
        /// <summary>
        /// Determine the target type for a member. This is intentionally designed to produce an error if a member has no target
        /// </summary>
        /// <param name="InMember"></param>
        /// <returns></returns>
        public static enumTargetType GetTargetType(MemberInfo InMember)
        {
            Object[] inObj = InMember.GetCustomAttributes(typeof(MM_Serializable_Target), true);
            if (inObj.Length == 0)
                return enumTargetType.None;
            else
                return (inObj[0] as MM_Serializable_Target).TargetType;
        }
        #endregion
    }
}
